package com.feiyi.app.fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.feiyi.app.R;
import com.feiyi.app.activity.LoginActivity;
import com.feiyi.app.activity.MyAddressActivity;
import com.feiyi.app.activity.MyFeedback;
import com.feiyi.app.activity.MyFocusActivity;
import com.feiyi.app.activity.MyJifenActivity;
import com.feiyi.app.activity.MyYouhuiActivity;
import com.feiyi.app.activity.PersonSettingActivity;
import com.feiyi.app.activity.orderlist.MyAllOrderListActivity;
import com.feiyi.app.activity.orderlist.MyAwaitFinishedOrderListActivity;
import com.feiyi.app.activity.orderlist.MyAwaitPayOrderListActivity;
import com.feiyi.app.activity.orderlist.MyAwaitReceiveOrderListActivity;
import com.feiyi.app.app.AppConstant;
import com.feiyi.app.base.BaseActivity;
import com.feiyi.app.base.BaseFragment;
import com.feiyi.app.bean.ChangeHeadBean;
import com.feiyi.app.event.ChangeHeadEvent;
import com.feiyi.app.event.LoginEvent;
import com.feiyi.app.glideutils.GlideImgManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 作者 : wlt
 * 创建时间:  2018-07-14
 * QQ : 1346947404
 * 邮箱:wanglt_android@163.com
 * 类说明:个人中心主界面
 * In me the tiger sniffs the rose
 */

public class MineFragment extends BaseFragment {

    @BindView(R.id.rl_login_regist)
    RelativeLayout rl_login_regist;
    @BindView(R.id.rl_name_certified)
    RelativeLayout rl_name_certified;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.person)
    CircleImageView person;
    public static boolean isAttatched = false;
    private static final String TAG = "main";
    private String avatar;

    @Override
    public View getLayoutInflater(LayoutInflater inflater) {

        View view = inflater.inflate(R.layout.fragment_mine_new,null);
        ButterKnife.bind(this,view);
        EventBus.getDefault().register(this);
        isAttatched = true;
        showOrHide();
        return view;
    }
    private void showOrHide(){
        avatar = BaseActivity.spUtils.getString(AppConstant.AVATAR);
        if (null != BaseActivity.spUtils.getString(AppConstant.NAME) && !BaseActivity.spUtils.getString(AppConstant.NAME).isEmpty()){
            rl_login_regist.setVisibility(View.GONE);
            rl_name_certified.setVisibility(View.VISIBLE);
            name.setText(BaseActivity.spUtils.getString(AppConstant.NAME));
            if (null != avatar && !avatar.isEmpty())
            GlideImgManager.loadUserImage(activity,avatar,person);
        }else {
            rl_login_regist.setVisibility(View.VISIBLE);
            rl_name_certified.setVisibility(View.GONE);

        }

    }

    @Override
    public void initView() {

    }

    @Override
    public void initDate() {

    }

    @Override
    public void initAdapter() {

    }

    @OnClick({R.id.rl_login_regist,R.id.iv_mine_setting,R.id.ll_mine_focus_goods,R.id.ll_mine_visit_site,R.id.rl_mine_reward_points,R.id.rl_mine_voucher,R.id.rl_mine_address,R.id.ll_mine_leave_something,R.id.ll_to_be_paid,R.id.ll_to_be_shipped,R.id.ll_to_be_evaluated,R.id.ll_all_orders})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.rl_login_regist:
                ActivityUtils.startActivity(LoginActivity.class);
                break;
            case R.id.iv_mine_setting:
                if (null != BaseActivity.spUtils.getString(AppConstant.NAME) && !BaseActivity.spUtils.getString(AppConstant.NAME).isEmpty()){

                    ActivityUtils.startActivity(PersonSettingActivity.class);
                }else {
                    ToastUtils.showShort("请登录");
                    ActivityUtils.startActivity(LoginActivity.class);
                }
                break;
            case R.id.ll_mine_focus_goods:
                if (null != BaseActivity.spUtils.getString(AppConstant.NAME) && !BaseActivity.spUtils.getString(AppConstant.NAME).isEmpty()){
                    Log.e(TAG, "sid= "+BaseActivity.spUtils.getString(AppConstant.SID) );
                    ActivityUtils.startActivity(MyFocusActivity.class);
                }else {
                    ToastUtils.showShort("请登录");
                    ActivityUtils.startActivity(LoginActivity.class);
                }
                break;
            case R.id.rl_mine_reward_points:
                if (null != BaseActivity.spUtils.getString(AppConstant.NAME) && !BaseActivity.spUtils.getString(AppConstant.NAME).isEmpty()){
                    Log.e(TAG, "sid= "+BaseActivity.spUtils.getString(AppConstant.SID) );
                    ActivityUtils.startActivity(MyJifenActivity.class);
                }else {
                    ToastUtils.showShort("请登录");
                    ActivityUtils.startActivity(LoginActivity.class);
                }
                break;
            case R.id.rl_mine_voucher:
                if (null != BaseActivity.spUtils.getString(AppConstant.NAME) && !BaseActivity.spUtils.getString(AppConstant.NAME).isEmpty()){
                    Log.e(TAG, "sid= "+BaseActivity.spUtils.getString(AppConstant.SID) );
                    ActivityUtils.startActivity(MyYouhuiActivity.class);
                }else {
                    ToastUtils.showShort("请登录");
                    ActivityUtils.startActivity(LoginActivity.class);
                }
                break;
            case R.id.rl_mine_address:
                if (null != BaseActivity.spUtils.getString(AppConstant.NAME) && !BaseActivity.spUtils.getString(AppConstant.NAME).isEmpty()){
                    Log.e(TAG, "sid= "+BaseActivity.spUtils.getString(AppConstant.SID) );
                    ActivityUtils.startActivity(MyAddressActivity.class);
                }else {
                    ToastUtils.showShort("请登录");
                    ActivityUtils.startActivity(LoginActivity.class);
                }
                break;
            case R.id.ll_mine_leave_something:
                if (null != BaseActivity.spUtils.getString(AppConstant.NAME) && !BaseActivity.spUtils.getString(AppConstant.NAME).isEmpty()){
                    Log.e(TAG, "sid= "+BaseActivity.spUtils.getString(AppConstant.SID) );
                    ActivityUtils.startActivity(MyFeedback.class);
                }else {
                    ToastUtils.showShort("请登录");
                    ActivityUtils.startActivity(LoginActivity.class);
                }
                break;
            case R.id.ll_to_be_paid:
                if (null != BaseActivity.spUtils.getString(AppConstant.NAME) && !BaseActivity.spUtils.getString(AppConstant.NAME).isEmpty()){
                    Log.e(TAG, "sid= "+BaseActivity.spUtils.getString(AppConstant.SID) );
                    ActivityUtils.startActivity(MyAwaitPayOrderListActivity.class);
                }else {
                    ToastUtils.showShort("请登录");
                    ActivityUtils.startActivity(LoginActivity.class);
                }
                break;
            case R.id.ll_to_be_shipped:
                if (null != BaseActivity.spUtils.getString(AppConstant.NAME) && !BaseActivity.spUtils.getString(AppConstant.NAME).isEmpty()){
                    Log.e(TAG, "sid= "+BaseActivity.spUtils.getString(AppConstant.SID) );
                    ActivityUtils.startActivity(MyAwaitReceiveOrderListActivity.class);
                }else {
                    ToastUtils.showShort("请登录");
                    ActivityUtils.startActivity(LoginActivity.class);
                }
                break;
            case R.id.ll_to_be_evaluated:
                if (null != BaseActivity.spUtils.getString(AppConstant.NAME) && !BaseActivity.spUtils.getString(AppConstant.NAME).isEmpty()){
                    Log.e(TAG, "sid= "+BaseActivity.spUtils.getString(AppConstant.SID) );
                    ActivityUtils.startActivity(MyAwaitFinishedOrderListActivity.class);
                }else {
                    ToastUtils.showShort("请登录");
                    ActivityUtils.startActivity(LoginActivity.class);
                }
                break;
            case R.id.ll_all_orders:
                if (null != BaseActivity.spUtils.getString(AppConstant.NAME) && !BaseActivity.spUtils.getString(AppConstant.NAME).isEmpty()){
                    Log.e(TAG, "sid= "+BaseActivity.spUtils.getString(AppConstant.SID) );
                    ActivityUtils.startActivity(MyAllOrderListActivity.class);
                }else {
                    ToastUtils.showShort("请登录");
                    ActivityUtils.startActivity(LoginActivity.class);
                }
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void showOrHideUser(LoginEvent event){
        avatar = BaseActivity.spUtils.getString(AppConstant.AVATAR);
        if (null != event.getRegisterBean().getData()){
            rl_login_regist.setVisibility(View.GONE);
            rl_name_certified.setVisibility(View.VISIBLE);
            name.setText(event.getRegisterBean().getData().getUser().getName());
            if (null != avatar && !avatar.isEmpty())
                GlideImgManager.loadUserImage(activity,avatar,person);
        }else {
            rl_login_regist.setVisibility(View.VISIBLE);
            rl_name_certified.setVisibility(View.GONE);
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void showHead(ChangeHeadEvent event){

        GlideImgManager.loadUserImage(activity,event.getHead(),person);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
        isAttatched = false;
    }
}
